package com.neu.questionnaire_system.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neu.questionnaire_system.entity.Questionnaire;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xingle
 * @since 2022-10-31
 */
@Mapper
public interface QuestionnaireMapper extends BaseMapper<Questionnaire> {

    //插入问卷
    int insertQuestionnaire(Map<String,Object> map);

    @Select("select * from questionnaire")
    List<Questionnaire> showQuestionnaire();

    @Delete("delete from questionnaire where qid=#{qid} and status=#{status}")
    int deleteQuestionnaire(@Param("qid")String qid,@Param("status")String status);

    @Select("select * from questionnaire where qid=#{qid}")
    List<Questionnaire> findQuestionnaireById(@Param("qid")String qid);

    /**
     * created by libo 返回创建好的问卷信息
     * @param qid 根据问卷的qid查询问卷
     * @return 返回问卷
     */
    @Select("select * from questionnaire where qid=#{qid}")
    Questionnaire findQuestionnaireByQid(@Param("qid") String qid);

    /**
     * created by libo 个性化组卷更新数据库数据
     * @param map 包含个性化组卷的数据
     * @return 返回int
     */
    int individualizationQues(Map<String,Object> map);

    //将问卷状态变为开启状态，表示发布问卷。或将问卷状态变为关闭，表示关闭问卷
    @Update("update questionnaire set status=#{status} where qid=#{qid}")
    int updateQuestionnaireStatus(@Param("status")String status,@Param("qid")String qid);

    IPage<Questionnaire> selectQuestionnaire(@Param("page") Page page, @Param("uid")String uid);
    @Select("select uid from questionnaire where qid=#{qid}")
    String findUidByQid(@Param("qid")String qid);

    @Select("select answeredCount from questionnaire where uid=#{uid,jdbcType=VARCHAR}")
    int findAnsweredCountByUid(@Param("uid")String uid);

    @Select("select (answeredCount/answerCount) as recyclingRate,qname,answerTime,answeredCount from questionnaire")
    List<Map<String,Object>> selectQuestionnaireDetail();

    @Select("select status from questionnaire where qid=#{qid}")
    String isQuestionnaireOpenByQid(@Param("qid")String qid);

    @Update("update questionnaire set answeredCount=#{answeredCount} where qid=#{qid}")
    int updateQuestionnaireCount(@Param("answeredCount")int count,@Param("qid")String qid);

}
